﻿
@{
    ViewBag.Title = "User";
    Layout = "~/Views/Shared/_MyLayout.cshtml";
}

<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
    <legend>人员信息管理</legend>
</fieldset>
<form class="layui-form" enctype="multipart/form-data">
    <div class="layui-form-item">
        <label class="layui-form-label">搜索</label>
        <div class="layui-input-block">
            <input type="text" id="SearchText" name="Title1" required lay-verify="required" onkeypress="getKey()" placeholder="输入用户名" class="layui-input" style="width:300px;float:left;">
            <input type="text" value="" style="display:none" />
            <button type="button" class="layui-btn layui-btn-normal" style="float:left;" onclick="SearchFunc()"><i class="layui-icon">&#xe615;</i></button>
            <button type="button" class="layui-btn layui-btn-danger" style="float:left;" id="BtnAddUser"><i class="layui-icon">&#xe61f;</i>新增</button>

            @*<button type="button" class="layui-btn layui-btn-normal" onclick="apply()"><i class="layui-icon">&#xe63b;</i></button>*@
        </div>
    </div>
</form>

<table id="demo" lay-filter="UserTableList"></table>

<div id="addLayer" style="display:none;">
    <form class="layui-form" id="AddFrom" style="margin-top:10px;">
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">用户名</label>
                <div class="layui-input-inline">
                    <input type="text" name="UserName" lay-verify="required" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">昵称</label>
                <div class="layui-input-inline">
                    <input type="text" name="NickName" lay-verify="required" class="layui-input">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">权限</label>
            <div class="layui-input-inline" style="width:120px;">
                <select name="Authority"  required lay-verify="required" >
                    <option value="2">一般用户</option>
                    <option value="3">监视</option>
                    <option value="1">管理员</option>
                </select>
            </div>
            <div class="layui-inline">
                <div class="layui-input-inline">
                    <label class="layui-form-label"></label>
                    <button type="button" class="layui-btn" id="BtnSave">保存</button>
                </div>
            </div>
        </div>
        <button type="submit" id="formAddsubmit" class="layui-btn" lay-submit lay-filter="formAddVerify" style="display:none;">立即提交</button>
    </form>
</div>

<div id="EditLayer" style="display:none;">
    <form class="layui-form" id="EditFrom" style="margin-top:10px;">
        <input type="text" name="UserId" style="display:none" />
        <div class="layui-form-item">

            <div class="layui-inline">
                <label class="layui-form-label">昵称</label>
                <div class="layui-input-inline">
                    <input type="text" name="NickName" lay-verify="required" class="layui-input">
                </div>
            </div>
            
            <div class="layui-inline">
                <label class="layui-form-label">权限</label>
                <div class="layui-input-inline" style="width:120px;">
                    <select name="Authority" id="EditAuthority" required lay-verify="required">
                        <option value="2">一般用户</option>
                        <option value="3">监视</option>
                        <option value="1">管理员</option>
                    </select>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <div class="layui-input-inline">
                    <label class="layui-form-label"></label>
                    <button type="button" class="layui-btn" id="BtnSaveEdit">保存</button>
                </div>
            </div>
        </div>
        <button type="submit" id="formEditsubmit" class="layui-btn" lay-submit lay-filter="formEditVerify" style="display:none;">立即提交</button>
    </form>
</div>
<div style="font-size:12px;margin-top:10px;margin-bottom:10px;margin-left:15px;width:100%;position: absolute;left: 0px;bottom: 0px;">
    ©2020 德器(重庆)科技有限公司|deqi(cq) technology.co.,ltd 服务热线 400-9019-505
    @*重庆环元环境科学研究院*@
</div>



<script type="text/javascript">

    var table;
    layui.use(['form','table', 'upload', 'layer'], function () {
        table = layui.table;
        var tab = parent.tab;
        var form = layui.form;
        var layer = layui.layer;
        var IsValidate = 0;
        var index;

        //拦截form提交消息
        form.on('submit(formAddVerify)', function (data) {
            //layer.msg(JSON.stringify(data.field));
            IsValidate = 1;
            return false;
        });
        //拦截form提交消息
        form.on('submit(formEditVerify)', function (data) {
            //layer.msg(JSON.stringify(data.field));
            IsValidate = 1;
            return false;
        });


        $("#BtnAddUser").on("click", function () {

            index = layer.open({
                type: 1,
                area: ['700px', '300px'],
                content: $("#addLayer") //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
            });
        });

        //新增用户保存
        $("#BtnSave").on("click", function () {
            $("#formAddsubmit").click();
            if (IsValidate == 1) {
                IsValidate = 0;
            } else {
                return;
            }
            var obj = $("#AddFrom").serializeArray();
            var data = {};
            $.each(obj, function (i, v) {
                data[v.name] = v.value;
            });
            $.ajax({
                type: "POST",
                url: "/Panel/UserAdd",
                async: false,
                contentType: "application/json",
                dataType: "json",
                data: JSON.stringify(data),
                success: function (msg) {
                    if (msg.result == "OK") {
                        layer.msg('新增成功', { icon: 6, time: 1000 });
                        layer.close(index);
                        table.reload("demo");
                    } else {
                        alert(msg.result);
                    }
                }
            });
        });

        //更新用户保存
        $("#BtnSaveEdit").on("click", function () {
            $("#formEditsubmit").click();
            if (IsValidate == 1) {
                IsValidate = 0;
            } else {
                return;
            }
            var obj = $("#EditFrom").serializeArray();
            var data = {};
            $.each(obj, function (i, v) {
                data[v.name] = v.value;
            });
            $.ajax({
                type: "POST",
                url: "/Panel/SaveUserById",
                async: false,
                contentType: "application/json",
                dataType: "json",
                data: JSON.stringify(data),
                success: function (msg) {
                    if (msg.result == "OK") {
                        layer.msg('修改成功', { icon: 6, time: 1000 });
                        layer.close(index);
                        table.reload("demo");

                    } else {
                        alert(msg.result);
                    }
                }
            });
        });


        //第一个实例
        table.render({
            elem: '#demo'
            , method: 'post'
            //, height: 315
            , url: '/Panel/UserTableRequest' //数据接口
            , page: true //开启分页
            , where: { SortName: "UserId", Order: "Desc", Condition: " and IsDelete<>1" }
            , limits: [10, 50, 100]

            , cols: [[ //表头
                { field: 'UserName', title: '用户名', width: 150, fixed: 'left' }
                , { field: 'NickName', title: '昵称', width: 150 }
                , {
                    field: "Authority", title: "权限", width: 100,
                    templet: function (d) {
                        if (d.Authority == 1) {
                            return "管理员";
                        }
                        else if (d.Authority == 2) {
                            return "一般用户";
                        }
                        else if (d.Authority == 3) {
                            return "监视";
                        }
                        
                    }
                }
                , {
                    field: "operation", title: "操作", width: 250,
                    templet: function (d) {
                        return '<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="RestPW">重置密码</a>' +
                            '<a class="layui-btn layui-btn-xs" lay-event="detail">编辑</a><a class="layui-btn layui-btn-xs" lay-event="TableDel">删除</a>';
                    }
                }
            ]]
        });

        table.on("tool(UserTableList)", function (obj) {
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的DOM对象

            if (layEvent == 'detail') {
                $.ajax({
                    type: "POST",
                    url: "/Panel/GetUserById",
                    async: false,
                    data: { "UserId": data.UserId },
                    success: function (msg) {
                        index = layer.open({
                            type: 1,
                            title: ['编辑', 'font-size:18px;'],
                            area: ['700px', '300px'],
                            content: $("#EditLayer"),  //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
                            success: function (layero, index) {
                                $("#EditFrom input[name='UserId']").val(data.UserId);
                                $("#EditFrom input[name='NickName']").val(msg.NickName);
                                $("#EditAuthority").val(msg.Authority);
                                form.render();
                            }
                        });
                    }
                });

            }

            if (layEvent == 'TableDel') {
                layer.confirm('<span class="layui-icon"></span>' + '是否真的要删除该用户？', {
                    btn: ['确定删除', '取消'] //可以无限个按钮
                    , btn1: function (index) {
                        //按钮【删除】的回调
                        //SSEditDelete
                        $.ajax({
                            type: "POST",
                            url: "/Panel/UserDeleteById",
                            async: false,
                            data: { "UserId": data.UserId },
                            success: function (msg) {
                                if (msg.result == "OK") {
                                    layer.msg('删除成功', { icon: 6, time: 1000 });
                                    table.reload("demo", {
                                        //where: { SortName: "SSlistId", Order: "Desc", Condition: " and 1=1" }
                                    });
                                    //tab.close(tab.getId());
                                } else {
                                    alert("删除失败");
                                }
                            }
                        });
                        layer.close(index);
                    }
                    , btn2: function (index) {
                        //按钮【按钮三】的回调
                        //alert("2");
                    }
                });
            }

            if (layEvent == 'RestPW') {
                layer.confirm('<span class="layui-icon"></span>' + '是否真的要重置该用户登录密码？', {
                    btn: ['确定重置', '取消'] //可以无限个按钮
                    , btn1: function (index) {
                        //按钮【删除】的回调
                        //SSEditDelete
                        $.ajax({
                            type: "POST",
                            url: "/Panel/UserResetPass",
                            async: false,
                            data: { "UserId": data.UserId },
                            success: function (msg) {
                                if (msg.result == "OK") {
                                    layer.msg('密码重置为"12345678"', { icon: 6, time: 1000 });
                                    table.reload("demo", {
                                        //where: { SortName: "SSlistId", Order: "Desc", Condition: " and 1=1" }
                                    });
                                    //tab.close(tab.getId());
                                } else {
                                    alert("重置失败");
                                }
                            }
                        });
                        layer.close(index);
                    }
                    , btn2: function (index) {
                        //按钮【按钮三】的回调
                        //alert("2");
                    }
                });
            }

        });

    });

    //回车键
    function getKey() {
        if (event.keyCode == 13) {
            SearchFunc();
        }
    }

    function SearchFunc() {
        var text = $("#SearchText").val();
        if (text == "") {
            table.reload("demo", {
                where: {
                    SortName: "UserId", Order: "Desc", Condition: " and IsDelete<>1"
                },
                page: {
                    curr: 1
                }
            });
        }
        else {
            table.reload("demo", {
                where: {
                    SortName: "UserId", Order: "Desc", Condition: " and IsDelete<>1 and UserName like '%" + text + "%'"
                },
                page: {
                    curr: 1
                }
            });
        }
    }

</script>

